﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Windows;

namespace Yxinmt.Windows
{
    public static class ViewConfig
    {
        public static Dictionary<string, Dictionary<string, object>> DictionaryView = new Dictionary<string, Dictionary<string, object>>();

        public static void LoadViewConfig()
        {
            try
            {
                DictionaryView = new Dictionary<string, Dictionary<string, object>>();

                string path = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "configs");

                string content = string.Empty;
                string name = string.Empty;

                DirectoryInfo direct = new DirectoryInfo(path);
                direct.GetFiles().ToList().ForEach(file =>
                {
                    content = string.Empty;
                    name = file.Name.Replace(file.Extension, string.Empty);

                    using (TextReader reader = file.OpenText())
                    {
                        content = reader.ReadToEnd();
                    }

                    if (!string.IsNullOrEmpty(content))
                    {
                        DictionaryView.Add(name, Utility.Common.JsonToDictionary(content));
                    }
                });

                if (DictionaryView.ContainsKey("AppSettings"))
                {
                    Utility.AppSetting.Data = DictionaryView["AppSettings"];
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static Dictionary<string, object> GetView(string name)
        {

            if (name.Equals("DefaultConfig")) return new Dictionary<string, object>();
            if (DictionaryView.ContainsKey(name)) return CloneDictionary(DictionaryView[name]);

#if DEBUG
            string appId = Utility.AppSetting.AppId;

            HttpClient client = new HttpClient();
            string content = client.GetStringAsync("http://118.25.18.233:85/configs/getconfig2?appId=" + appId + "&name=" + name).Result;
            if (!string.IsNullOrEmpty(content))
            {
                return CloneDictionary(Utility.Common.JsonToDictionary(content));
            }
            else throw new Exception(string.Format("未找到{0}视图配置！", name));

#else
            if (DictionaryView.ContainsKey(name)) return CloneDictionary(DictionaryView[name]);
            throw new Exception(string.Format("未找到{0}视图配置！", name));
#endif
        }

        public static Dictionary<string, object> CloneDictionary(Dictionary<string, object> dict)
        {
            return CloneDictionary(dict, null);
        }

        public static Dictionary<string, object> CloneDictionary(Dictionary<string, object> dict, List<Entity.PropertySet> propertyList)
        {
            Dictionary<string, object> dict2 = new Dictionary<string, object>();
            Entity.PropertySet property = null;
            bool blExists = false;

            string key = dict.ContainsKey("name") ? dict["name"].ToString() : string.Empty;

            foreach (var kvp in dict)
            {
                if (kvp.Value is Dictionary<string, object>)
                {
                    blExists = false;
                    if (!string.IsNullOrEmpty(key) && propertyList != null)
                    {
                        property = propertyList.Where(w => w.Key == key && kvp.Key == w.Name).FirstOrDefault();
                        if (property != null) { blExists = true; }
                    }
                    if (!blExists)
                    {
                        dict2[kvp.Key] = CloneDictionary(kvp.Value as Dictionary<string, object>, propertyList);
                    }
                }
                else if (kvp.Value is List<Dictionary<string, object>>)
                {

                    blExists = false;
                    if (!string.IsNullOrEmpty(key) && propertyList != null)
                    {
                        property = propertyList.Where(w => w.Key == key && kvp.Key == w.Name).FirstOrDefault();
                        if (property != null) { blExists = true; }
                    }

                    if (!blExists) dict2[kvp.Key] = (from a in kvp.Value as List<Dictionary<string, object>>
                                                     select CloneDictionary(a, propertyList)).ToList();
                }
                else
                {
                    dict2[kvp.Key] = kvp.Value;
                }
            }

            if (propertyList != null && !string.IsNullOrEmpty(key))
            {
                propertyList.Where(w => w.Key == key).ToList().ForEach(p =>
                {
                    if (p.IsPropertyName && p.Value != null && dict2.ContainsKey((string)p.Value)) dict2[p.Name] = dict2[(string)p.Value];
                    else dict2[p.Name] = p.Value;
                });
            }

            return dict2;
        }
    }
}
